﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.Security;
using System.Security.Cryptography;

using System.Text.RegularExpressions;

namespace Business
{
    public partial class PermissionsAttributes : Form
    {
        public MainForm m_mainform;

        public string sname;

        public string sname1;
        public string sname2;
        public string sname3;


        public TreeNode nd1;
        public TreeNode nd2;
        public TreeNode nd3;
        public PermissionsAttributes()
        {
            InitializeComponent();
            getsetTreeNode(treeView1.Nodes);
        }
        public void updatemoqx(string name,string ss,string sleixing)
        {

            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"Select count(*) From mokuaiquanxian where qx_name='{0}'", name);
            if (objquery.ExecuteS(ssql))
            {
                if (sleixing.Contains("警员"))
                {
                    ssql = string.Format(@"update  mokuaiquanxian set qx_jingyuan='{0}' where qx_name='{1}'", ss, name); ;
                }
                else if (sleixing.Contains("核心"))
                {
                    ssql = string.Format(@"update  mokuaiquanxian set qx_hxgn='{0}' where qx_name='{1}'", ss, name); ;
                }
            }
            else
            {
                ssql = string.Format(@"insert into mokuaiquanxian (qx_name,qx_jingyuan) values ('{0}','{1}')", name, ss);
            }
            objquery.ExecuteS(ssql);
        }
        private void suoyouquanxian_Load(object sender, EventArgs e)
        {
            // treeView2.ExpandAll();  展开全部
            treeView2.Nodes[0].Expand();  //默认展开第一个节点
            treeView2.Nodes[1].Expand();  //默认展开第一个节点
            treeView2.Nodes[2].Expand();  //默认展开第一个节点
            treeView2.Nodes[3].Expand();  //默认展开第一个节点

            treeView1.Nodes[0].Expand();  //默认展开第一个节点
            treeView1.Nodes[1].Expand();  //默认展开第一个节点
            treeView1.Nodes[3].Expand();  //默认展开第一个节点
            treeView1.Nodes[26].Expand();  //默认展开第一个节点
            treeView1.Nodes[27].Expand();  //默认展开第一个节点
        }
        private void getallTreeNode(TreeNodeCollection nodes)
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                sname1 = td.Text;
                if (td.Nodes.Count > 0)
                    getallTreeNodezl2(td.Nodes);
            }
        }
        private void getallTreeNodezl2(TreeNodeCollection nodes)//子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                sname2 = td.Text;
                if (td.Nodes.Count > 0)
                {
                    getallTreeNodezl3(td.Nodes);
                }
                else
                {
                     string sxuanzhong="否" ;
                    if(td.Checked)
                    {
                        sxuanzhong="是" ;
                    }
                    updatemoqx(sname1, sxuanzhong, td.Text);
                }

            }
        }
        private void getallTreeNodezl3(TreeNodeCollection nodes)//三级子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                sname3 = td.Text;
                bool bche = td.Checked;
                 if (td.Nodes.Count > 0)
                {
                    getallTreeNodezl3(td.Nodes);
                }
                else
                {
                     string sxuanzhong="否" ;
                    if(td.Checked)
                    {
                        sxuanzhong="是" ;
                    }
                    updatemoqx(sname1 + sname2, sxuanzhong, td.Text);
                }
            }
        }

       

        public void getset()
        {
 
        }
        public bool getmoqx(string name,  string sleixing)
        {

            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"Select * From mokuaiquanxian where qx_name='{0}'", name);
            string ziduan = "";
            if (sleixing.Contains("警员"))
            {
                ziduan = "qx_jingyuan";
            }
            else
            {
                ziduan = "qx_hxgn";
            }
            string sjieguo = objquery.GetDBString(ssql, ziduan);
            if (sjieguo.Contains("是"))
            {
                return true;
            }
            else
            {
                return false;
            }
         
               
          
        }

        private void getsetTreeNode(TreeNodeCollection nodes)
        {
            
            foreach (TreeNode td in nodes)
            {
                nd1 = td;
                //  td.Text 读取该node;
                sname1 = td.Text;
                if (td.Nodes.Count > 0)
                    getsetTreeNode2(td.Nodes);
            }
        }

        private void getsetTreeNode2(TreeNodeCollection nodes)//子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                nd2 = td;
                sname2 = td.Text;
                if (td.Nodes.Count > 0)
                {
                    getsetTreeNode3(td.Nodes);
                }
                else
                {
                   
                   
                    if (getmoqx(sname1, td.Text))
                    {
                        td.Checked = true;
                        nd1.Expand(); 
                    }
                    else
                    {
                        td.Checked = false;
                    }
                }

            }
        }
        private void getsetTreeNode3(TreeNodeCollection nodes)//三级子类
        {
            foreach (TreeNode td in nodes)
            {
                //  td.Text 读取该node;
                nd3 = td;
                sname3 = td.Text;
                bool bche = td.Checked;
                if (td.Nodes.Count > 0)
                {
                    getsetTreeNode3(td.Nodes);
                }
                else
                {
                    if (getmoqx(sname1+sname2, td.Text))
                    {
                        td.Checked = true;
                        nd2.Expand(); 
                    }
                    else
                    {
                        td.Checked = false;
                    }
                }
            }
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            getallTreeNode(treeView1.Nodes);

            //保存操作日记
            m_mainform.savecaozuoriji("update  mokuaiquanxian set qx_jingyuan='是' where qx_name='业务管理'", "更新", "系统管理", "警员与核心功能属性修改");

            MessageBox.Show("保存成功");
        }


    }
}
